package com.zshy.base.mq.serialization.impl;

import com.zshy.base.core.utils.JsonKit;
import com.zshy.base.mq.serialization.MQEventSerialization;
import org.springframework.stereotype.Component;

/**
 * 默认json序列化实现
 * @author zhouhengzhe
 */
@Component("JsonMQEventSerialization")
public class JsonMQEventSerialization implements MQEventSerialization {

    /**
     * 反序列化（json）
     *
     * @param src
     * @param dist
     * @param <S>
     * @param <T>
     * @return
     * @throws RuntimeException
     */
    @Override
    public <S, T> T deserialize(S src, Class<T> dist) throws RuntimeException {
        if (src == null || ((String) src).isEmpty()) {
            return null;
        }
        return JsonKit.toObject(src, dist);
    }

    /**
     * 序列化（json）
     *
     * @param src
     * @return
     * @throws RuntimeException
     */
    @Override
    public String serialize(Object src) throws RuntimeException {
        return JsonKit.toJson(src);
    }

}